var DailyModule = function () {
    var editable = function () {

        return {
            init: function () {
                var oTable = $('#modelEditable').dataTable({
                	"autoWidth":false,
                	"scrollX": true,
                	"lengthMenu": [
                                   [10, 15, 20, 50],
                                   [10, 15, 20, 50]
                               ],
                    "displayLength": 10,
                    "language": {
                        "lengthMenu": "每页 _MENU_ 条记录",
                        "paginate": {
                            "previous": "上一页",
                            "next": "下一页"
                        },
                        "info" : "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "记录数为0",
                     },
                     "columnDefs": [
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [0]
            						},
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [1]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [2]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [3]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [4]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [5]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [6]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [7]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [8]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [9]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [10]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [11]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [12]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [13]
            						}
                                ],
                                
                     "columns": [
                                { "data": "paySerial" },
                                { "data": "investorName" },
                                { "data": "productBriefName"},
                                { "data": "payAmount" },
                                { "data": "withdrawAmount" },
                                { "data": "posHandleFee" },
                                { "data": "payTime" },
                                { "data": "auditState" },
                                { "data": "bankDeposit" },
                                { "data": "bankName" },
                                { "data": "bankAccount" },
                                { "data": "auditTime" },
                                { "data": "employeeName" }],
                   "filter" : false,
                   "processing": false,
                   "serverSide": true,
                   "ajaxSource": ctx + "/dailyPay/query",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   nRow.children[0].id = aData.id;
                	   /*$('td:eq(1)', nRow).html('<a href="investor/detail/'+aData.investorId+'">'+aData.investorName+'</a>');
                	   $('td:eq(2)', nRow).html('<a href="product/detail/'+aData.productId+'">'+aData.productBriefName+'</a>');*/
                	   
                	   $('td:eq(3)', nRow).html(formatCurrency(aData.payAmount)+"&nbsp;元");
                	   $('td:eq(4)', nRow).html(formatCurrency(aData.withdrawAmount)+"&nbsp;元");
                	   $('td:eq(5)', nRow).html(formatCurrency(aData.posHandleFee)+"&nbsp;元");
                	   
                	   var auditState;
                	   var opera ;
                	   if(aData.auditState == null || aData.auditState == 0){
                		   auditState = "未审核";
                		   opera = '<a class="detail fa fa-bars fa-lg" title="详情" href="javascript:;"></a>&nbsp;&nbsp;&nbsp;<a class="confirm fa fa-money fa-lg" title="确认清算" href="javascript:;"></a>';
                	   }else if(aData.auditState == 1){
                		   auditState = "已到账";
                		   opera = '<a class="detail fa fa-bars fa-lg" title="详情" href="javascript:;"></a>';
                	   }
                	   
                	   $('td:eq(7)', nRow).html(auditState);
                	   $('td:eq(13)', nRow).html(opera);
                       return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
	                   var json = "";
	                   var s;
	                   for (var i = 0; i < dataSet.length; i++) {
	                       s = dataSet[i];
	                   	   if (s.name == 'sEcho') {
	                   		   json = json + '"echo":' + s.value;
		                   } else if (s.name == 'iDisplayStart') {
		                	   json = json + ',"start":' + s.value;
		                   } else if (s.name == 'iDisplayLength') {
		                	   json = json + ',"pageSize":' + s.value;
		                   }
	                   }
	                   var form = $("form#queryForm").serializeJSON();
                   	   var js = form.replace('}', ',');
                   	   json = js + json + "}";
                       $.ajax({
                       	"contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ajaxSource,
                           "data" : json,
                           "success" : fnCallback
                       });
                   },
                });
                // 确认清算
                $('#modelEditable').on("click", "a.confirm", function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var obj = nRow.children[0];
                    var paySerial = obj.innerHTML;
                    var id = obj.id;
                    json = "{\"id\":\""+id+"\"}";
                    bootbox.confirm("确认已经将该支付流水提交给银联?请再次确认该流水"+paySerial+"存在清算Exel并已提交给中国银联.", function(result) {
                        if (result) {
                            $.ajax({
                            	type:"post",
                            	contentType:"application/json; charset=UTF-8",
                                url: ctx + "/dailyPay/confirm",
                                data:json,
                                dataType:"json",
                                success: function(msg){
                                	if (msg.code == 1) {
                                		bootbox.alert("确认清算信息成功！");
                                		$('#modelEditable').dataTable().fnUpdate('auditState' , nRow[0], 1 );
                                        $('#modelEditable').dataTable().fnUpdate('auditTime' , nRow[0], 1 );
                                        $('#modelEditable').dataTable().fnUpdate('employeeName' , nRow[0], 1 );
                                	} else {
                                		bootbox.alert("确认清算信息失败！");
                                	}
                                },
                                error: function (msg) {
                                	bootbox.alert(msg.message);
                                }
                            });
                        }
                    });
                });
                /* // 编辑
                $('#modelEditable').on("click", "button.edit", (function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    
                    
                }));*/
                // 明细
                $('#modelEditable').on("click", "a.detail", (function (e) {
                    e.preventDefault();

                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                	
                    window.location.href = "paySerial/dailyDetail/"+id;
                }));
                
                /*$('#modelEditable').on("click", "button.relatedFile", (function (e) {
                    e.preventDefault();

                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                	
                    window.location.href = "productAttachment/relatedFile/"+id;
                }));
                
                $('#modelEditable').on("click", "button.commission", (function (e) {
                    e.preventDefault();

                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                	
                    window.location.href = "product/commission/"+id;
                }));
                
                $('#modelEditable').on("click", "button.net", (function (e) {
                    e.preventDefault();
                    
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    
                    window.location.href = "productNet/net/"+id;
                }));
                
                $('#modelEditable').on("click", "button.ralatedProduct", (function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    
                    window.location.href = "productRelated/list/"+id;
                }));*/
                
                $('#queryBtn').click(function(){
                	oTable.fnClearTable();
                });
                
                $('#exportBtn').click(function(){
                	window.location.href = "dailyPay/export?payStartTime="+$("#payStartTime").val()+"&type=0";
                });
               /* $('#addBtn').click(function(){
                	window.location.href = "product/add";
                });*/
            }

        };

    }();
    
    return {
        init: function () {
        	editable.init();
        }
    };
}();

$(function(){
	 //每日清算的支付时间默认时间是当前时间
    var date = new Date();
    var strYear = date.getFullYear();    
    var strDay = date.getDate();    
    var strMonth = date.getMonth()+1;
    if(strDay <= 9){
    	strDay = "0"+strDay;
    }
    if(strMonth <= 9){
    	strMonth = "0"+strMonth;
    }
    datastr = strYear+"-"+strMonth+"-"+strDay;  
    $("#payStartTime").val(datastr);
	$("#queryBtn").click();
})